/*!
 * Ext JS Library 3.3.1
 * Copyright(c) 2006-2010 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */
NotesViewerWindow = Ext.extend(Ext.app.Module, {
    id: 'notesviewer-win',
    autostart: false,
    store: null,
    init: function() {
        this.launcher = {
            text: 'Notes Viewer Window',
            iconCls: 'refwin-icon',
            handler: this.createWindow,
            scope: this
        };
    },

//TODO: reload notes!!

     createWindow: function(baseUri) {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('notesviewer-win');
       	
       	// create the data store
	    var store = new Ext.data.JsonStore({
	    	autoDestroy: true,
	    	totalProperty: 'results',
	    	root: 'rows',
	    	url: './references?action=getNotes&uri=' + baseUri,
	    	fields: ['id', 'text']
	    });
	    this.store = store;
       	
   		// Panel for the west
		var notesGrid = new Ext.grid.GridPanel({
		    region: 'west',
		    split: true,
		    width: 120,
		    collapsible: false,
		    margins:'3 0 3 3',
		    cmargins:'3 3 3 3',
		    /*tbar: [{
	            iconCls:'add-note',
	            text:'Add Note',
	            handler: this.showWindow,
	            scope: this
	        },{
	            id:'delete-note',
	            iconCls:'delete-icon',
	            text:'Remove Note',
	            handler: function(){
	                var s = this.getSelectionModel().getSelectedNode();
	                if(s){
	                    alert('TODO: removeNote');//this.removeNote(s.attributes.url);
	                }
	            },
	            scope: this
	        }],*/
		    store: store,
	        columns: [{
	                id       :'note-id',
	                header   : 'Note-ID',
					width: '100%',
	                sortable : true,
	                dataIndex: 'id'
	            }]
		});
		
		//left mouseclick opens the note text in the noteView-Panel
	    notesGrid.addListener('rowclick', function(notesGrid, rowIndex, e) {
        	e.stopEvent(); 
        	notesGrid.getSelectionModel().selectRow(rowIndex); 
        	var record = notesGrid.getStore().getAt(rowIndex);
        	//Opens the clicked Website in a new BrowserWindow
			noteView.get('noteText').setValue(record.data.text);
        });
       	
       	 // panel for showing the note's text
		var noteView = new Ext.Panel({
		    region: 'center',
		    margins: '3 3 3 0',
		    defaults:{autoScroll:true},
		    items:[{
		    	id: 'noteText',
		        name: 'text',
		        fieldLabel: 'Text',
		        xtype: 'textarea',
		        width: '100%',
		        height: '100%'
		    }]
		});
		
		store.load();
       	
		if (!win) {
            win = desktop.createWindow({
                id: 'notesviewer-win',
                title: 'Notes Viewer Window',
                width: 400,
                height: 300,
                x: 700,
                y: 200,
                iconCls: 'refwin-icon',
                shim: false,
                animCollapse: false,
                border: false,
                constrainHeader: true,
                plain: true,
            	layout: 'border',
                items: [ notesGrid, noteView ]
            });
        }
        win.show();

     }
});

